package com.hoow.shakoow.shake.components.imageView

import android.os.Bundle
import android.view.*
import androidx.fragment.app.DialogFragment
import androidx.fragment.app.FragmentManager
import androidx.viewpager.widget.ViewPager
import com.hoow.shakoow.R
import com.hoow.shakoow.ai.data.RevObjUserAblum
import com.hoow.shakoow.base.BaseDialogAction
import com.hoow.shakoow.databinding.ViewShakeImageGallaryBinding
import com.hoow.shakoow.shake.adapter.UserFeedbackImage

class ShakeImageGallary : DialogFragment(), BaseDialogAction {

    companion object {
        //点击保存相册
        const val ACTION_SAVE_ALBUM = 1

        //点击保存相册
        const val ACTION_TRY_AGAIN = 2

        //点击关闭
        const val ACTION_CLOSE = 0

        fun showDialog(
            fragmentManager: FragmentManager,
            dataList: List<RevObjUserAblum>,
            index: Int,
            onClickListener: (Int) -> Unit = {},
        ) {
            val dialog = ShakeImageGallary()
            dialog.mIndex = index

            // 转化
            val targetList = mutableListOf<String>()
            for (element in dataList) {
                element.imgUrl?.let { vUrl ->
                    targetList.add(vUrl)
                }
            }

            dialog.mDataSource = targetList
            dialog.mOnClickActionListener = onClickListener
            dialog.showNow(fragmentManager, ShakeImageGallary::class.java.simpleName)
        }

        fun showDialogForFeedbackImages(
            fragmentManager: FragmentManager,
            dataList: List<UserFeedbackImage>,
            index: Int,
            onClickListener: (Int) -> Unit = {},
        ) {
            val dialog = ShakeImageGallary()
            dialog.mIndex = index

            // 转化
            val targetList = mutableListOf<String>()
            for (element in dataList) {
                element.imgUrl?.let { vUrl ->
                    targetList.add(vUrl)
                }
            }

            dialog.mDataSource = targetList
            dialog.mOnClickActionListener = onClickListener
            dialog.showNow(fragmentManager, ShakeImageGallary::class.java.simpleName)
        }
    }

    lateinit var mViewBinding: ViewShakeImageGallaryBinding

    //列表适配器
    private lateinit var mImageGallaryAdapter: ShakeImageGallaryAdapter

    //数据源
    private var mDataSourceUserAblum: List<RevObjUserAblum>? = null

    //数据源
    private var mDataSource: List<String>? = null

    //当前显示定位
    private var mIndex: Int = 0

    //回调
    private var mOnClickActionListener: (Int) -> Unit = {}

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?,
    ): View {
        dialog?.requestWindowFeature(Window.FEATURE_NO_TITLE);
        mViewBinding =
            ViewShakeImageGallaryBinding.inflate(layoutInflater)

        return mViewBinding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        dialog?.setCancelable(true)
        dialog?.setCanceledOnTouchOutside(true)
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setStyle(STYLE_NO_TITLE, R.style.Dialog)
    }

    override fun onStart() {
        super.onStart()

        initSubviews()

        dialog?.window?.let {
            it.attributes.gravity = Gravity.CENTER
            it.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)
//            it.setWindowAnimations(R.style.bottomDialogAnimation)
        }

    }

    override fun initSubviews() {
        try {
            //设置返回事件
            mViewBinding.topNavAiImageGallary.setOnActionClickListener {
                dismiss()
            }

            //初始化列表配置
            mDataSource?.let {

                //设置当前/总数
                var topCenterText = "${mIndex + 1}/${it.size}"
                if (it.size < 2) {
                    topCenterText = ""
                }
                mViewBinding.topNavAiImageGallary.setCenterText(
                    topCenterText
                )

                mImageGallaryAdapter =
                    ShakeImageGallaryAdapter(requireContext(), it)
                mViewBinding.viewPagerAiImageGallary.adapter =
                    mImageGallaryAdapter
                mViewBinding.viewPagerAiImageGallary.pageMargin = 0
                mViewBinding.viewPagerAiImageGallary.offscreenPageLimit = 3
                mViewBinding.viewPagerAiImageGallary.addOnPageChangeListener(
                    object : ViewPager.OnPageChangeListener {
                        override fun onPageScrolled(
                            position: Int,
                            positionOffset: Float,
                            positionOffsetPixels: Int
                        ) {
                        }

                        override fun onPageSelected(position: Int) {
                            //设置当前/总数
                            if (it.size > 1) {
                                val topCenterTextFromPageChanged = "${position + 1}/${it.size}"
                                mViewBinding.topNavAiImageGallary.setCenterText(
                                    topCenterTextFromPageChanged
                                )
                            }
                        }

                        override fun onPageScrollStateChanged(state: Int) {
                        }
                    })

                //配置点击事件
                mImageGallaryAdapter.setItemClickListener { action, revObjAiUseResult, bitmap, position ->
                    if (0 == action) {
                        //保存相册

                    } else if (1 == action) {
                        //重试

                    } else if (2 == action) {
                        //点击图片
                        mOnClickActionListener.invoke(position)
                        dismiss()
                    }
                }

                mViewBinding.viewPagerAiImageGallary.setCurrentItem(
                    mIndex,
                    false
                )
            }

        } catch (e: Exception) {
            e.printStackTrace()
        }
    }

    override fun showWithUITag(
        gTag: String,
        fragmentManager: FragmentManager
    ) {

    }

    override fun setOnActionClickListener(listener: (Int) -> Unit) {
        mOnClickActionListener = listener
    }

}